<?php
namespace Company\Controller;
use Think\Controller;
class WarehouseController extends BaseController {
	public function _initialize()
    {
        parent::_initialize();
        $g=session('company');
		C('erp_dbname',$g['db']);
        $db_name=$g['db'];
		//数据库连接基础配置-begin
		C('db_config.DB_TYPE',C('DB_TYPE'));
		C('db_config.DB_HOST',C('DB_HOST'));
		C('db_config.DB_USER',C('DB_USER'));
		C('db_config.DB_PWD',C('DB_PWD'));
		C('db_config.DB_PORT',C('DB_PORT'));
		C('db_config.DB_CHARSET',C('DB_CHARSET'));
		C('db_config.DB_NAME',C('erp_dbname'));
		//数据库连接基础配置-end
        
    }
	
    public function index(){
        $g=session('company');
        $user_id=$g['company_id'];
        $warehouse_id=M("user_warehouse")->where("user_id='".$user_id."'")->getField('warehouse_group_id');
        $w_id=explode(',',$warehouse_id);
        if(I("get.name")===null || I("get.name")===''){
            $where['id']=array('in',$w_id);
        }else{
            $where['id']=array('in',$w_id);
            $where['name']=trim(I("get.name"));
        }
        if(I("get.sort")!=''){
            $order=I("get.sort")." ".I("get.dec");
        }else{
            $order='id desc';
        }
        $warehouse = M('warehouse');
        $count      = $warehouse->where($where)->count();
        $Page       = new \Think\Page($count,15);
        $show       = $Page->show();
        $list = $warehouse->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        $this->assign('page',$show);
        $this->count=$count;
        $this->display();
    }
    public function getWarehouse(){
        $list= M('unit')->select();
        if($list){
            alert(1,$list);
        }else{
            alert(0,'暂无');
        }
    }
	//保质期列表
	public function get_warehousebound_overduetime(){
		$param=I('param.','','trim');
		$warehouse_code=$param['warehouse_code'];
		$manua_id=$param['manua_id'];
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$bar_code=$param['bar_code'];
		$start_time=$param['start_time'];//开始时间
		$end_time=$param['end_time'];//结束时间
		$excel=I('excel');
		
		if(!empty($start_time)){
			$url_arr['start_time']=$start_time;
		}
		if(!empty($end_time)){
			$url_arr['end_time']=$end_time;
		}
		if(!empty($warehouse_code)){
			$url_arr['warehouse_code']=$warehouse_code;
		}
		if(!empty($goods_no)){
			$url_arr['goods_no']=$goods_no;
		}
		if(!empty($goods_name)){
			$url_arr['goods_name']=$goods_name;
		}
		if(!empty($bar_code)){
			$url_arr['bar_code']=$bar_code;
		}
		if(!empty($manua_id)){
			$url_arr['manua_id']=$manua_id;
		}
		
		$data['warehouse_code']=$warehouse_code;
		$data['goods_no']=$goods_no;
		$data['goods_name']=$goods_name;
		$data['bar_code']=$bar_code;
		$data['manua_id']=$manua_id;
		$data['start_time']=$start_time;
		$data['end_time']=$end_time;
		
		$p=$param['p'];
		$rows=20;
		if(!empty($p)){
			$url_arr['p']=$p;
		}
		if($status==2){
			$rows=5000;
		}
		
		$data['page']=$p;
		$data['rows']=$rows;
		$data['sort']='id';
		$data['order']='desc';

		$excel_url=U('Warehouse/get_warehousebound_batch',$url_arr);
		
		$url=C('BASE_CONFIG.warehousebound_batch');
		$return=push($url,$data);
		$list=json_decode($return,true);
		// dump($list);
		// die;
		$Page       = new \Think\Page($list['total'],$data['rows']);
        $show       = $Page->show();
		
		$ids=$param['ids'];
		if(!empty($ids)){
			$idss=explode(',',$ids);
			foreach($list['rows'] as $k=>$v){
				foreach($idss as $ks=>$vs){
					if($v['id']==$vs){
						$arr[]=$v;
						break;
					}
				}
			}
			$list['rows']=$arr;
		}
		$pro=M('product',C('DB_PREFIX_T'),C('db_config'));
		foreach($list['rows'] as $ks=>$vs){
			$list['rows'][$ks]['current_type']=$pro->where(array('goods_no'=>$vs['goods_no']))->getField('currency');
		}
		if($excel){
			foreach($list['rows'] as $ks=>$vs){
				$list['rows'][$ks]['goods_no']=' '.$vs['goods_no'];
				$list['rows'][$ks]['manua_id']=' '.$vs['manua_id'];
				$list['rows'][$ks]['bar_code']=' '.$vs['bar_code'];
			}
			$xlsName  = "商品库存列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('goods_cate','商品类型'),
				array('bar_code','条形码'),
				array('goods_spec','规格'),
				array('unit1','第一单位'),
				array('unit2','第二单位'),
				array('goods_price','入库平均价格'),
				array('amount','总数量'),
				array('storage_amount','库存数量'),
				array('freez_amount','冻结数量'),
				array('conforming_p','合格品'),
				array('bad_product','残次品'),
				// array('scrap_one','报废品'),
				array('push_times','推送次数'),
				array('info_count','批次数量'),
			);
			$Aexcel=A('Order');
			$Aexcel->exportExcel($xlsName,$xlsCell,$list['rows']);
			die;
		}
		
		$this->assign('excel_url',$excel_url);
		$this->assign('page',$show);
		$this->assign('total',$list['total']);
		$this->assign('list',$list['rows']);
		// dump($return);
		$this->display();
	}
	//仓库库存列表
	public function get_warehousebound(){
		$param=I('param.','','trim');
		// dump($param);
		// die;
		$warehouse_code=$param['warehouse_code'];
		$manua_id=$param['manua_id'];
		$goods_no=$param['goods_no'];
		$goods_name=$param['goods_name'];
		$bar_code=$param['bar_code'];
		$goods_cate=$param['goods_cate'];
		$status=$param['status'];

		$excel=I('excel');
		if(!empty($warehouse_code)){
			$url_arr['warehouse_code']=$warehouse_code;
		}
		if(!empty($goods_no)){
			$url_arr['goods_no']=$goods_no;
		}
		if(!empty($goods_name)){
			$url_arr['goods_name']=$goods_name;
		}
		if(!empty($bar_code)){
			$url_arr['bar_code']=$bar_code;
		}
		if(!empty($goods_cate)){
			$url_arr['goods_cate']=$goods_cate;
		}
		if(!empty($status)){
			$url_arr['status']=$status;
		}
		if(!empty($manua_id)){
			$url_arr['manua_id']=$manua_id;
		}
		
		
		
		$data['warehouse_code']=$warehouse_code;
		$data['goods_no']=$goods_no;
		$data['goods_name']=$goods_name;
		$data['bar_code']=$bar_code;
		$data['goods_cate']=$goods_cate;
		
		$data['manua_id']=$manua_id;
		
		$p=$param['p'];
		$rows=20;
		if(!empty($p)){
			$url_arr['p']=$p;
		}
		if($status==2){
			$rows=5000;
		}
		
		$data['page']=$p;
		$data['rows']=$rows;
		$data['sort']='id';
		$data['order']='desc';

		$excel_url=U('Warehouse/get_warehousebound',$url_arr);
		
		$url=C('BASE_CONFIG.warehousebound');
		$return=push($url,$data);
		$list=json_decode($return,true);
		
		$Page       = new \Think\Page($list['total'],$data['rows']);
        $show       = $Page->show();
		
		$ids=$param['ids'];
		if(!empty($ids)){
			$idss=explode(',',$ids);
			foreach($list['rows'] as $k=>$v){
				foreach($idss as $ks=>$vs){
					if($v['id']==$vs){
						$arr[]=$v;
						break;
					}
				}
			}
			$list['rows']=$arr;
		}
		// dump($list);
		// die;
		$pro=M('product',C('DB_PREFIX_T'),C('db_config'));
		foreach($list['rows'] as $ks=>$vs){
			$list['rows'][$ks]['current_type']=$pro->where(array('goods_no'=>$vs['goods_no']))->getField('currency');
		}
		if($excel){
			foreach($list['rows'] as $ks=>$vs){
				$list['rows'][$ks]['goods_no']=' '.$vs['goods_no'];
				$list['rows'][$ks]['manua_id']=' '.$vs['manua_id'];
				$list['rows'][$ks]['bar_code']=' '.$vs['bar_code'];
			}
			$xlsName  = "商品库存列表";
			$xlsCell  = array(
				array('goods_no','商品编码'),
				array('manua_id','账册编号'),
				array('goods_name','商品名称'),
				array('goods_cate','商品类型'),
				array('bar_code','条形码'),
				array('goods_spec','规格'),
				array('unit1','第一单位'),
				array('unit2','第二单位'),
				array('goods_price','入库平均价格'),
				array('amount','总数量'),
				array('storage_amount','库存数量'),
				array('freez_amount','冻结数量'),
				array('conforming_p','合格品'),
				array('bad_product','不良品'),
				// array('scrap_one','报废品'),
				array('push_times','推送次数'),
				array('info_count','批次数量'),
			);
			$Aexcel=A('Order');
			$Aexcel->exportExcel($xlsName,$xlsCell,$list['rows']);
			die;
		}
		
		$this->assign('excel_url',$excel_url);
		$this->assign('page',$show);
		$this->assign('total',$list['total']);
		$this->assign('list',$list['rows']);
		// dump($return);
		$this->display();
	}
}